package com.hezhi.trafficcopsys.util;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import com.hezhi.trafficcopsys.domain.User;
import com.hezhi.trafficcopsys.persistence.dao.UserDao;
import com.hezhi.trafficcopsys.persistence.dao.impl.UserDaoImpl;
import com.opensymphony.xwork2.util.logging.Logger;
import com.opensymphony.xwork2.util.logging.LoggerFactory;
public final class ApplicationListener implements ServletContextListener {

    // ------------------------------------------------------ Manifest Constants


    /**
     * <p>Application scope attribute key under which the valid selection
     * items for the protocol property is stored.</p>
     */
    public static final String PROTOCOLS_KEY = "protocols";

    // ------------------------------------------------------ Instance Variables


    /**
     * <p>The <code>ServletContext</code> for this web application.</p>
     */
    private ServletContext context = null;


    /**
     * <p>Logging output for this plug in instance.</p>
     */
    private Logger log = LoggerFactory.getLogger(this.getClass());

    // ------------------------------------------------------------- Properties

    // ------------------------------------------ ServletContextListener Methods


    /**
     * <p>Gracefully shut down this database, releasing any resources that
     * were allocated at initialization.</p>
     *
     * @param event ServletContextEvent to process
     */
    public void contextDestroyed(ServletContextEvent event) {

        log.info("Finalizing SQLite database plug in");
        context.removeAttribute(PROTOCOLS_KEY);
        context = null;
    }


    /**
     * <p>Initialize and load our initial database from persistent
     * storage.</p>
     *
     * @param event The context initialization event
     */
    public void contextInitialized(ServletContextEvent event) {

        log.info("Initializing SQLite database plug in from");

        // Remember our associated ServletContext
        this.context = event.getServletContext();
        
        UserDao userDao = new UserDaoImpl();
        
        if( userDao.getTotalSize() == 0){
        	User user = new User();
        	user.setUsername("admin");
        	user.setPassword("123");
        	user.setPrivilege("1111");
        	user.setFilepath("C://tmp//search_admin.txt");
        	userDao.addUser(user);
        }
        
    }
}
